package dao

import (
	"blogger/global"
	"blogger/model"
	"errors"
)

// 添加文章
func AddArticle(detail *model.ArticleDetail) error {
	sqlStr := "insert into article(title, content, about, create_time) values(?, ?, ?, ?)"
	// 获取stme
	stmt, err := global.DB.Prepare(sqlStr)
	if err != nil {
		panic("获取mysql连接错误")
	}
	_, err = stmt.Exec(detail.Title, detail.Content, detail.About, detail.CreateTime)
	if err != nil {
		return errors.New("添加失败")
	}
	return nil
}

// 通过文章id获取详情页
func GetArticleDetailById(id int) (*model.ArticleDetail, error) {
	articleDetail := model.ArticleDetail{}
	sqlStr := "SELECT id, content, title, about, create_time FROM article WHERE id = ?"
	err := global.DB.QueryRow(sqlStr, id).Scan(&articleDetail.Id, &articleDetail.Content, &articleDetail.Title, &articleDetail.About, &articleDetail.CreateTime)
	if err != nil {
		return &articleDetail, errors.New("查询错误")
	}
	return &articleDetail, nil
}